package com.ichart.html;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.ichart.doc.Component;
import com.ichart.doc.ComponentUtils;
import com.ichart.doc.Event;
import com.ichart.doc.Info;
import com.ichart.doc.Item;
import com.ichart.doc.Method;

public class PageTemplate extends Template<Component> {
	public static Pattern HREF = Pattern.compile("href='(\\w*).html",Pattern.DOTALL);
	public static Pattern INNERLINK = Pattern.compile("<link>(\\w*)(</link>|<link>)",Pattern.MULTILINE);
	public static Pattern OUTLINK1 = Pattern.compile("<link>(\\w*)\\.(\\w*)(</link>|<link>)",Pattern.MULTILINE);
	public static Pattern OUTLINK2 = Pattern.compile("<link>(\\w*)\\.(\\w*)#(\\w*)(</link>|<link>)",Pattern.MULTILINE);
	public PageTemplate(){
		
	}
	
	@Override
	protected void summary() {
		html.append("<div class='ichartjs_p_5'>");
		html.append("<div class='ichartjs_title ichartjs_radius2 ichartjs_p_5'>");
		
		html.append("<div class='ichartjs_inline ichartjs_left'>");
		html.append(Info.info("Overview"));
		html.append("&nbsp;&nbsp;");
		html.append("</div>");
		
		html.append("<a class='ichartjs_anchor' href='#configs'>");
		html.append(Info.info("Configs"));
		html.append("</a>");
		
		html.append("<a class='ichartjs_anchor' href='#events'>");
		html.append(Info.info("Events"));
		html.append("</a>");
		
		html.append("<a class='ichartjs_anchor' href='#methods'>");
		html.append(Info.info("Methods"));
		html.append("</a>");
		
		
		html.append("</div>");
		
		html.enter("<div class='ichartjs_overview_content'>");
		
		html.append("<span class='ichartjs_class'>");
		html.append(ComponentUtils.getClassName(this.object.getName()));
		html.enterBr("</span>&nbsp;&nbsp;");
		
		HtmlApi.invokeHierarchy(this.object.getName(),html);
		
		
		html.enter(this.object.getOverview());
		
		html.append("</div>");
		html.append("</div>");
	}
	private String link(String desc){
		desc = INNERLINK.matcher(desc).replaceAll("<a href='#ichart_link_"+"$1'>$1</a>");
		desc = OUTLINK1.matcher(desc).replaceAll("<a href='$1_$2.html'>$1.$2</a>");
		desc = OUTLINK2.matcher(desc).replaceAll("<a href='$1_$2.html#ichart_link_"+"$3'>$1.$2&nbsp;$3</a>");
		
		Matcher matcher = HREF.matcher(desc);
		while(matcher.find()){
			desc = desc.replaceAll(matcher.group(1), matcher.group(1).toLowerCase());
		}
		
		return desc;
	}
	/**
	 * build Option Configs
	 * void
	 * create at 2012-5-11 下午01:47:51
	 * @author wanghe
	 */
	private void option(){
		List<Item> items = HtmlApi.getAllItems(this.object.getName());
		html.append("<div class='ichartjs_p_5' id='configs' name='configs'>");
		html.append("<div class='ichartjs_title ichartjs_p_5'>");
		html.append(Info.info("Configs"));
		html.append("</div>");
		
		html.append("<table class='ichartjs_table'>");
		html.append("<tr class='ichartjs_table_header'>");
		html.append("<th class='ichartjs_table_parameter'>");
		html.append(Info.info("Parameter"));
		html.append("</th>");
		html.append("<th class='ichartjs_table_type'>");
		html.append(Info.info("Type"));
		html.append("</th>");
		html.append("<th class='ichartjs_table_description'>");
		html.append(Info.info("Description"));
		html.append("</th>");
		
		html.append("<th class='ichartjs_table_definedBy'>");
		html.append(Info.info("Defined By"));
		html.append("</th>");
		html.append("</tr>");
		int i =0;
		for (Item item : items) {
			if(i++%2==0){
				html.append("<tr class='ichartjs_even'>");
			}else{
				html.append("<tr class='ichartjs_odd'>");
			}
			
			html.append("<td id='ichart_link_");
			//html.append(item.getDefinedBy().toLowerCase()+"_");
			html.append(item.getAttribute());
			html.append("'>");
			html.append(item.getAttribute());
			html.append("</td>");
			
			html.append("<td>");
			html.append(link(item.getType()));
			html.append("</td>");
			
			
			html.append("<td>");
			html.append(link(SPLITROW.matcher(item.getAnnotation()).replaceAll("$1<br/>")));
			
			html.append("</td>");
			
			html.append("<td class='ichartjs_right ichartjs_stress'>");
			
			html.append(ComponentUtils.getLink(item.getFullName()));
			
			html.append("</td>");
			
			html.append("</tr>");
		}
		html.append("</table>");
		html.append("</div>");
	}
	
	/**
	 * build Events
	 * void
	 * create at 2012-5-11 下午01:48:15
	 * @author wanghe
	 */
	private void event(){
		List<Event> events  = HtmlApi.getAllEvents(this.object.getName());
		html.append("<div class='ichartjs_p_5' id='events' name='events'>");
		html.append("<div class='ichartjs_title ichartjs_p_5'>");
		html.append(Info.info("Events"));
		html.append("</div>");
		
		html.append("<table class='ichartjs_table'>");
		html.append("<tr class='ichartjs_table_header'>");
		html.append("<th class='ichartjs_table_parameter'>");
		html.append(Info.info("Name"));
		html.append("</th>");
		html.append("<th class='ichartjs_table_description'>");
		html.append(Info.info("Description"));
		html.append("</th>");
		
		html.append("<th class='ichartjs_table_definedBy'>");
		html.append(Info.info("Defined By"));
		html.append("</th>");
		html.append("</tr>");
		int i =0;
		for (Event event : events) {
			if(i++%2==0){
				html.append("<tr class='ichartjs_even'>");
			}else{
				html.append("<tr class='ichartjs_odd'>");
			}
			
			html.append("<td>");
			html.append(event.getMethod());
			html.append("</td>");
			
			html.append("<td>");
			html.append("<div class='ichartjs_h_25 ichartjs_pl_10'>");
			html.append(link(SPLITROW.matcher(event.getAnnotation()).replaceAll("$1<br/>")));
			html.enter("</div>");
			//
			
			html.append("<div class='ichartjs_h_25 ichartjs_pl_10'>");
			html.append("<div class='ichartjs_stress'>");
			html.enterBr(Info.info("Parameter"));
			html.enter("</div>");
			
			html.append("<div class='ichartjs_pl_10'><ul>");
			for(Item item:event.getParamters()){
				html.append("<li>");
				html.append(item.getAttribute());
				html.append("&nbsp;:&nbsp;");
				if(item.getAttribute().equals("this")){
					html.append(this.object.getClassName());
				}else{
					html.append(link(item.getType()));
				}
				
				html.append("&nbsp;&nbsp;");
				html.append(link(item.getAnnotation()));
				html.enter("</li>");
			}
			
			html.enter("</ul></div></div>");
			html.append("</td>");
			
			html.append("<td class='ichartjs_right ichartjs_stress'>");
			html.append(ComponentUtils.getLink(event.getFullName()));
			html.append("</td>");
			
			html.append("</tr>");
		}
		html.append("</table>");
		html.append("</div>");
	}
	
	
	/**
	 * build method
	 * void
	 * create at 2012-6-12 下午06:07:56
	 * @author wanghe
	 */
	private void method(){
		List<Method> methods  = HtmlApi.getAllMethods(this.object.getName());
		html.append("<div class='ichartjs_p_5' id='methods' name='methods'>");
		html.append("<div class='ichartjs_title ichartjs_p_5'>");
		html.append(Info.info("Methods"));
		html.append("</div>");
		
		html.append("<table class='ichartjs_table'>");
		html.append("<tr class='ichartjs_table_header'>");
		html.append("<th class='ichartjs_table_parameter'>");
		html.append(Info.info("Name"));
		html.append("</th>");
		html.append("<th class='ichartjs_table_description'>");
		html.append(Info.info("Description"));
		html.append("</th>");
		
		html.append("<th class='ichartjs_table_definedBy'>");
		html.append(Info.info("Defined By"));
		html.append("</th>");
		html.append("</tr>");
		int i =0;
		for (Method method : methods) {
			if(i++%2==0){
				html.append("<tr class='ichartjs_even'>");
			}else{
				html.append("<tr class='ichartjs_odd'>");
			}
			
			html.append("<td>");
			html.append(method.getMethod());
			html.append("</td>");
			
			html.append("<td>");
			html.append("<div class='ichartjs_h_25 ichartjs_pl_10'>");
			html.append(link(SPLITROW.matcher(method.getAnnotation()).replaceAll("$1<br/>")));
			html.enter("</div>");
			//
			
			html.append("<div class='ichartjs_h_25 ichartjs_pl_10'>");
			html.append("<div class='ichartjs_stress'>");
			html.enterBr(Info.info("Parameter"));
			html.enter("</div>");
			
			html.append("<div class='ichartjs_pl_10'><ul>");
			for(Item item:method.getParamters()){
				html.append("<li>");
				html.append(link(item.getAttribute()));
				html.append("&nbsp;:&nbsp;");
				if(item.getAttribute().equals("this")){
					html.append(this.object.getClassName());
				}else{
					html.append(item.getType());
				}
				html.append("&nbsp;&nbsp;");
				html.append(link(item.getAnnotation()));
				html.enter("</li>");
			}
			if(method.getParamters().size()==0){
				html.append("<li>");
				html.enterBr(Info.info("None"));
				html.enter("</li>");
			}
			html.enter("</ul></div>");
			
			try {
				html.append("<div class='ichartjs_stress'>");
				html.enterBr(Info.info("Return"));
				html.enter("</div>");
				html.append("<div class='ichartjs_pl_10'>");
				html.append("&nbsp;&nbsp;");
				html.append(link(method.getReturnValue().getAnnotation().replaceAll("#","&nbsp;:&nbsp;")));
				html.enter("</div>");
			} catch (Exception e) {
				System.out.println("The method not return value");
				e.printStackTrace();
			}
			
			html.enter("</div>");
			html.append("</td>");
			
			html.append("<td class='ichartjs_right ichartjs_stress'>");
			html.append(ComponentUtils.getLink(method.getFullName()));
			html.append("</td>");
			
			html.append("</tr>");
		}
		html.append("</table>");
		html.append("</div>");
	}
	
	@Override
	protected void detail() {
		option();
		event();
		method();
	}

	@Override
	protected void reference() {
		html.append("<div class='ichartjs_reference'>");
		html.append("<strong>");
		html.append(Info.info("reference"));
		html.append(":</strong>");
		
		html.append("<span class='ichartjs_reference_item'>");
		if(this.object.getExtend().equalsIgnoreCase("object")){
			html.append("none");
		}else{
			html.append(ComponentUtils.getLink(this.object.getExtend()));
		}
		
		html.append("</span>");
		html.append("</div>");
	}

	@Override
	protected String page() {
		Html html = new Html();
		html.enter("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
		html.enter("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
		html.enter("<head>");
		html.append("<title>");
		html.append(this.object.getName());
		html.append("-Document");
		html.enter("</title>");
		html.enter(this.theme);
		html.enter("</head>");
		html.enter("<body class='ichartjs_page'>");
		html.enter(this.html.toString());
		html.enter("</body>");
		html.append("</html>");
		return html.toString();
	}
	
	
		
}
